package com.mzj.saas.commons;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Author: wangrl
 * @Description: TODO
 * @Date: 19:52 2018/7/27
 * @Version: 1.0
 */
public class RandomNoUtil {

    /**
     * 输入number 生成指定规格的随机码
     * @param firstNumber 产生的位数
     * @param secondNumber 产生的位数
     * @return
     * @throws Exception
     */
    public static String getRandomNo(Integer firstNumber,Integer secondNumber) throws Exception{
        StringBuilder stringBuilder = new StringBuilder();
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String seconds = new SimpleDateFormat("HHmmss").format(new Date());
        stringBuilder.append(date);
        stringBuilder.append(getRandomNum(firstNumber));
        stringBuilder.append(seconds);
        stringBuilder.append(getRandomNum(secondNumber));
        return stringBuilder.toString();
    }

    /**
     * 获取number个随机数
     * @param number 随机数个数
     * @return
     */
    private static String getRandomNum(Integer number){
        StringBuffer sb = new StringBuffer();
        String str = "0135792468";
        Random r = new Random();
        for(int i=0;i<number;i++){
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num)+""), "");
        }
        return sb.toString();
    }

    /**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}
